﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using DimOS.Constants;
using DimOS.Internals;

// ReSharper disable AccessToStaticMemberViaDerivedType
namespace DimOS.VTS
{
    public static class Console
    {
        public static Screen CurrentScreen { get; set; }
        
        public static int X
        {
            get { return Screen.GetPosition().X; }
            set { if (CurrentScreen != null) CurrentScreen.X = value; Screen.PosX = value; }
        }

        public static int Y
        {
            get { return Screen.GetPosition().Y; }
            set { if (CurrentScreen != null) CurrentScreen.Y = value; Screen.PosY = value; }
        }

        public static ConsoleColor Foreground
        {
            get { return Screen.GetForeground(); }
            set { if (CurrentScreen != null) CurrentScreen.Foreground = value; Screen.SetForeground(value); }
        }

        public static ConsoleColor Background
        {
            get { return Screen.GetBackground(); }
            set { if (CurrentScreen != null) CurrentScreen.Background = value; Screen.SetBackground(value); }
        }

        public static byte ScrollStatus
        { get { return CurrentScreen != null ? CurrentScreen.ScrollStatus : (byte)4; } }

        public static void SetColor(int x, int y, ConsoleColor foreground, ConsoleColor background)
        {
            if (CurrentScreen != null) CurrentScreen[x, y].Color =
                (byte)((byte)(foreground) | ((byte)(background) << 4));
            Screen.SetForeground(x, y, foreground);
            Screen.SetBackground(x, y, background);
        }

        public static void SetCursorSize(int size)
        {
            if (CurrentScreen != null) CurrentScreen.CursorSize = size;
            Screen.SetCursorSize(size);
        }

        public static void ShowCursor()
        {
            if (CurrentScreen != null) CurrentScreen.CursorVisibility = true;
            Screen.ShowCursor();
        }

        public static void HideCursor()
        {
            if (CurrentScreen != null) CurrentScreen.CursorVisibility = false;
            Screen.HideCursor();
        }

        public static int Rows { get { return Screen.TotalRows; } }
        public static int Columns { get { return Screen.TotalColumns; } }

        public static void Clear()
        {
            if (CurrentScreen != null) CurrentScreen.Clear();
            Screen.ClearOutput();
        }

        public static void ClearFull()
        {
            if (CurrentScreen != null) CurrentScreen.Clear();
            Screen.ClearFull();
        }

        private static void NewLine()
        {
            X = 0; Y++;
            if (Y >= Rows - 2)
            { Y = Rows - 3; if (CurrentScreen != null) CurrentScreen.Scroll(true); else Screen.ScrollForward(); }
        }

        public static void WriteLine(string text = null, bool wrapLine = true,
            bool scrollEnd = false, bool mainScreenOnly = false)
        { Write(text, wrapLine, scrollEnd, mainScreenOnly); NewLine(); }

        public static void Write(string text, bool wrapLine = true,
            bool scrollEnd = false, bool mainScreenOnly = false)
        {
            if (CurrentScreen != null && scrollEnd) CurrentScreen.ScrollEnd(true);
            if (text == null) return;
            for (int i = 0; i < text.Length; i++)
            {
                if (text[i] == Tokens.NewLine) NewLine();
                else if (text[i] == '\t') Write("    ");
                else if (text[i] != '\r')
                {
                    if (CurrentScreen != null && !mainScreenOnly) CurrentScreen[X,Y] = new CharColor(text[i], Screen.GetColor());
                    Screen.SetCharacter(X, Y, text[i]); if (wrapLine || i < text.Length - 1) { if (X == Columns) NewLine(); X++; }
                }
            }
        }

        public static ConsoleColor GetForeground(int x, int y)
        {
            if (CurrentScreen != null) return (ConsoleColor)(CurrentScreen[x, y].Color & 0xF);
            return (ConsoleColor)(Screen.GetColor(x, y) & 0xF);
        }

        public static ConsoleColor GetBackground(int x, int y)
        {
            if (CurrentScreen != null) return (ConsoleColor)((CurrentScreen[x, y].Color & 0xF0 >> 4));
            return (ConsoleColor)((Screen.GetColor(x, y) & 0xF0) >> 4);
        }

        public static char GetCharacter(int x, int y)
        {
            if (CurrentScreen != null) return CurrentScreen[x, y].Character;
            return Screen.GetCharacter(x, y);
        }

    }
}
